package com.cr.api.controller;

import com.cr.api.vo.LawyerInfoVo;
import com.cr.api.vo.UserVo;
import com.cr.business.domain.CrLawyerInfo;
import com.cr.business.domain.CrUser;
import com.cr.business.service.ICrLawyerInfoService;
import com.cr.business.service.ICrUserService;
import com.cr.common.core.controller.BaseController;
import com.cr.common.core.domain.AjaxResult;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 问答Controller
 * 
 * @author G
 * @date 2021-09-14
 */
@Api(tags = "律师服务", value = "律师服务")
@RestController
@CrossOrigin
@RequestMapping("/api/lawyer")
public class ApiLawyerController extends BaseController
{
    @Autowired
    private ICrUserService userService;
    @Autowired
    private ICrLawyerInfoService lawyerInfoService;
    /**
     * 问题列表
     */
    @ApiOperation("律师列表")
    @PostMapping("/getLawyerList")
    @ResponseBody
    public AjaxResult getLawyerList()
    {
        CrLawyerInfo lawyer = new CrLawyerInfo();
        startPage();
        List<CrLawyerInfo> info = lawyerInfoService.selectCrLawyerInfoList(lawyer);
        List<LawyerInfoVo> userVoList = new ArrayList<>();
        for(CrLawyerInfo u : info){
            LawyerInfoVo vo = new LawyerInfoVo();
            BeanUtils.copyProperties(u, vo);
            userVoList.add(vo);
        }
        return AjaxResult.success(new PageInfo<>(userVoList));
    }

    /**
     * 律师详情
     * @param userId
     * @return
     */
    @ApiOperation("律师详情")
    @ApiImplicitParam(name = "userId", value = "律师ID", required = true, dataType = "int")
    @PostMapping("/getInfo")
    @ResponseBody
    public AjaxResult getInfo(@RequestBody CrUser param){
        Long userId = param.getUserId();
        if(userId == null){
            return AjaxResult.error("律师ID不能为空");
        }
        CrLawyerInfo info = lawyerInfoService.selectCrLawyerInfoByUserId(userId);
        LawyerInfoVo lawyerInfoVo = new LawyerInfoVo();
        if(Objects.nonNull(info)){
            BeanUtils.copyProperties(info,lawyerInfoVo);
        }
        return AjaxResult.success(lawyerInfoVo);
    }
}
